package cn.itsource.ymcc.domain;

import cn.itsource.ymcc.constants.BaseConstants;
import com.baomidou.mybatisplus.enums.IdType;
import java.math.BigDecimal;
import java.util.Date;
import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.activerecord.Model;
import com.baomidou.mybatisplus.annotations.TableName;
import lombok.Data;

import java.io.Serializable;

/**
 * <p>
 * 
 * </p>
 *
 * @author whale
 * @since 2021-10-16
 */
@TableName("t_kill_course")
@Data
public class KillCourse extends Model<KillCourse> {

    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.AUTO)
    private Long id;
    /**
     * 课程名字
     */
    @TableField("course_name")
    private String courseName;
    /**
     * 对应的课程ID
     */
    @TableField("course_id")
    private Long courseId;
    @TableField("kill_price")
    private BigDecimal killPrice;
    /**
     * 库存
     */
    @TableField("kill_count")
    private Integer killCount;
    /**
     * 每个人可以秒杀的数量,默认1
     */
    @TableField("kill_limit")
    private Integer killLimit = 1;
    /**
     * 秒杀课程排序
     */
    @TableField("kill_sort")
    private Integer killSort;
    /**
     // 秒杀状态:0待发布，1已发布 2秒杀下架
     */
    @TableField("publish_status")
    private Integer publishStatus = 0;

    @TableField("course_pic")
    private String coursePic;

    @TableField("start_time")
    private Long startTime;

    @TableField("end_time")
    private Long endTime;

    @TableField("time_str")
    private String timeStr;
    /**
     * 创建时间
     */
    @TableField("create_time")
    private Date createTime;
    /**
     * 发布时间
     */
    @TableField("publish_time")
    private Date publishTime;

    /**
     * 讲师
     */
    @TableField("teacher_names")
    private String teacherNames;
    /**
     * 活动ID
     */
    @TableField("activity_id")
    private Long activityId;

    //秒杀码
    @TableField(exist = false)
    private String killCode;

    //秒杀时间字符串
    public String getBeginTimeStr(){
        return BaseConstants.SIMPLE_DATE_FORMAT.format(new Date(getStartTime()));
    }
    public String getEndTimeStr(){
        return BaseConstants.SIMPLE_DATE_FORMAT.format(new Date(getEndTime()));
    }
    public String getTime(){
        return BaseConstants.DATE_FORMAT_MM_DD_HOURSE_MINI.format(new Date(startTime));
    }

    //秒杀名字
    public String getKillStatusName(){
        if(publishStatus == BaseConstants.Kill.STATUS_PUBLISH_WAIT){
            return "待发布";
        }
        if(publishStatus == BaseConstants.Kill.STATUS_OFF_LINE){
            return "已经下架";
        }
        if(publishStatus == BaseConstants.Kill.STATUS_PUBLISHING){
            //发布中
            if(isKilling()) return "秒杀中";
            if(isUnbegin()) return "未开始";
            return "秒杀结束";
        }
        return "未知状态";
    }

    //秒杀中
    public boolean isKilling(){
        Date date = new Date();
        return date.after(new Date(startTime)) && date.before(new Date(endTime)) ;
    }

    //是否未开始，未开始返回true
    public boolean isUnbegin(){
        return  new Date().before(new Date(startTime));
    }

    //秒杀开始时间差
    public Long getTimeDiffMill(){
        //未开始
        if(isUnbegin()){
            return (startTime - new Date().getTime()) / 1000;
        }
        //秒杀中
        if(isKilling()){
            return (endTime - new Date().getTime())/1000;
        }
        //秒杀结束
        return 0l;
    }


    @Override
    protected Serializable pkVal() {
        return this.id;
    }

}
